﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Data.SqlTypes;

namespace DataAccess.Operate
{
    public class DA_Project_Confirmation_Operate:SqlInteractionBase
    {
        private DataEntity.T_Project_Confirmation _objMessage = null;
        public override bool Insert(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Project_Confirmation_Insert]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = (DataEntity.T_Project_Confirmation)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Confirmation_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Confirmation_CommissionID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_CommissionID));
                cmdToExecute.Parameters.Add(new SqlParameter("@sProject_Confirmation_NO", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_NO));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcProject_Confirmation_Amount", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 1, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_Amount));
                cmdToExecute.Parameters.Add(new SqlParameter("@daProject_Confirmation_Date", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_Date));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcProject_Confirmation_DeveloperAmount", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 1, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_DeveloperAmount));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcProject_Confirmation_PartCommission", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 1, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_PartCommission));
                cmdToExecute.Parameters.Add(new SqlParameter("@daProject_Confirmation_CommissionDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_CommissionDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@iProject_Confirmation_Status", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_Status));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Project_Confirmation::Insert::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }


        public override object SelectOne(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Project_Confirmation_SelectOne]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;
            DataTable toReturn = new DataTable("t_Project_Confirmation");
            SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);
            

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = new DataEntity.T_Project_Confirmation();
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Confirmation_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, SqlGuid.Parse(obj.ToString())));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                adapter.Fill(toReturn);
                if (toReturn.Rows.Count > 0)
                {
                    _objMessage.Project_Confirmation_ID = (Guid)toReturn.Rows[0]["Project_Confirmation_ID"];
                    //_objMessage.Project_Confirmation_CommissionID = (Guid)toReturn.Rows[0]["Project_Confirmation_CommissionID"];
                    //_objMessage.Project_Confirmation_NO = (string)toReturn.Rows[0]["Project_Confirmation_NO"];
                    //_objMessage.Project_Confirmation_Amount = (Decimal)toReturn.Rows[0]["Project_Confirmation_Amount"];
                    //_objMessage.Project_Confirmation_Date = (DateTime)toReturn.Rows[0]["Project_Confirmation_Date"];
           
                    //_objMessage.Project_Confirmation_DeveloperAmount = toReturn.Rows[0]["Project_Confirmation_DeveloperAmount"] == System.DBNull.Value ? SqlDecimal.Null : (Decimal)toReturn.Rows[0]["Project_Confirmation_DeveloperAmount"];
                    //_objMessage.Project_Confirmation_PartCommission = toReturn.Rows[0]["Project_Confirmation_PartCommission"] == System.DBNull.Value ? SqlDecimal.Null : (Decimal)toReturn.Rows[0]["Project_Confirmation_PartCommission"];
                    //_objMessage.Project_Confirmation_CommissionDate = toReturn.Rows[0]["Project_Confirmation_CommissionDate"] == System.DBNull.Value ? SqlDateTime.Null : (DateTime)toReturn.Rows[0]["Project_Confirmation_CommissionDate"];
                    _objMessage.Project_Confirmation_Status = toReturn.Rows[0]["Project_Confirmation_Status"] == System.DBNull.Value ? SqlInt32.Null : (Int32)toReturn.Rows[0]["Project_Confirmation_Status"];
                }
                return this._objMessage;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Project_Confirmation::SelectOne::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
                adapter.Dispose();
            }
        }



        public override bool Update(object obj)
        {
            SqlCommand cmdToExecute = new SqlCommand();
            cmdToExecute.CommandText = "dbo.[sp_t_Project_Confirmation_Update]";
            cmdToExecute.CommandType = CommandType.StoredProcedure;

            // Use base class' connection object
            cmdToExecute.Connection = _mainConnection;
            this._objMessage = (DataEntity.T_Project_Confirmation)obj;
            try
            {
                cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Confirmation_ID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_ID));
                cmdToExecute.Parameters.Add(new SqlParameter("@guidProject_Confirmation_CommissionID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_CommissionID));
                cmdToExecute.Parameters.Add(new SqlParameter("@sProject_Confirmation_NO", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_NO));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcProject_Confirmation_Amount", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 1, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_Amount));
                cmdToExecute.Parameters.Add(new SqlParameter("@daProject_Confirmation_Date", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_Date));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcProject_Confirmation_DeveloperAmount", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 1, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_DeveloperAmount));
                cmdToExecute.Parameters.Add(new SqlParameter("@dcProject_Confirmation_PartCommission", SqlDbType.Decimal, 9, ParameterDirection.Input, false, 18, 1, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_PartCommission));
                cmdToExecute.Parameters.Add(new SqlParameter("@daProject_Confirmation_CommissionDate", SqlDbType.DateTime, 8, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_CommissionDate));
                cmdToExecute.Parameters.Add(new SqlParameter("@iProject_Confirmation_Status", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _objMessage.Project_Confirmation_Status));

                if (_mainConnectionIsCreatedLocal)
                {
                    // Open connection.
                    _mainConnection.Open();
                }
                else
                {
                    if (_mainConnectionProvider.IsTransactionPending)
                    {
                        cmdToExecute.Transaction = _mainConnectionProvider.CurrentTransaction;
                    }
                }

                // Execute query.
                _rowsAffected = cmdToExecute.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                // some error occured. Bubble it to caller and encapsulate Exception object
                throw new Exception("T_Project_Confirmation::Update::Error occured.", ex);
            }
            finally
            {
                if (_mainConnectionIsCreatedLocal)
                {
                    // Close connection.
                    _mainConnection.Close();
                }
                cmdToExecute.Dispose();
            }
        }
    }
}